import axios from 'axios';
import Vue from 'vue';

// 导出Excel公用方法
export function exportMethod(data) {
    console.log(Vue.prototype.$host + data.url)
    axios({
        method: data.method,
        url:  Vue.prototype.$host + `${data.url}${data.params ? '?' + data.params : ''}`,
        headers:{
            "X-Token":localStorage.getItem("X-Token"),
        },
        responseType: 'blob'
    }).then((res) => {
        const link = document.createElement('a')
        let blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
 
        // link.download = res.headers['content-disposition'] //下载后文件名
        link.download = data.fileName //下载的文件名
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
    }).catch(error => {
        console.log(error)
    })
}